using System;
using System.Collections;
using System.Collections.Generic;
using DDA;
using Sirenix.OdinInspector;
using Survivor;
using UnityEngine;
using Object = System.Object;

public abstract class DDAAction : DDATreeNodeBase
{
    public override int NextNodeMaxNum  => 0;
}

public class DDAIncreaseEnemyGenerateNum : DDAAction
{
    [ShowInInspector, LabelText("怪物一次生成数量的增量")]
    public int increment = 1;

    public override bool Invoke()
    {
        base.Invoke();
        EnemyGenerator.Instance.generateNum += this.increment;
        Global.DDALog.Value += $"\n{Mathf.Floor(Global.CurrentTime.Value)} EnemyGenerateNumber+{increment}";
        return true;
    }
}

public class DDAIncreaseEnemyHealthScale : DDAAction
{
    [ShowInInspector, LabelText("怪物血量增量")]
    public int increment = 1;

    public override bool Invoke()
    {
        base.Invoke();
        EnemyGenerator.Instance.enemyHealthScale += 1;
        Global.DDALog.Value += $"{Mathf.Floor(Global.CurrentTime.Value)}  \nSet Enemy HealthScale {EnemyGenerator.Instance.enemyHealthScale}";
        return true;
    }
}

public class DDASetEnemyHealthScaleNeedInputNumber : DDAAction,IInputObject
{
    [ShowInInspector, LabelText("敌人生命倍率")] private float _healthScale = 1;
    public override bool Invoke()
    {        
        base.Invoke();
        EnemyGenerator.Instance.enemyHealthScale = _healthScale;

        return true;
    }

    public Type InputType()
    {
        return typeof(float);
    }

    public void ReceiveObject(object obj)
    {
        this._healthScale = (float)obj;
    }
}




public  class DDASetAbilityWeight:DDAAction,IInputObject
{
    [ShowInInspector, LabelText("能力")] public AbilityEnum abilityEnum;
    [ShowInInspector, LabelText("权重")] public int weight;
    [ShowInInspector, LabelText("是否是增量")] public bool isIncrement = false;
    
#if UNITY_EDITOR
    public override string Description => "设置能力出现的权重，能够接受一个int变量，表示对应的能力。";
#endif

    public override string NodeName => abilityEnum switch
    {
        AbilityEnum.SimpleAttack => "设置普通攻击权重",
        AbilityEnum.SimpleAxe => "设置斧头攻击权重",
        AbilityEnum.SimpleCircle => "设置环绕攻击权重",
        AbilityEnum.SimpleKnife => "设置小刀攻击权重",
        AbilityEnum.Critical=>"设置暴击率权重",
        AbilityEnum.Damage=>"设置伤害权重",
        AbilityEnum.FlyThingCount=>"设置发射物书数量权重",
        AbilityEnum.MoveSpeed => "设置移动速度权重",
        AbilityEnum.PickUpRange => "设置拾取范围权重",
        AbilityEnum.Exp => "设置经验权重",
        _ => "Null"
    };

    public override bool Invoke()
    {      
        base.Invoke();
        var system = Global.Interface.GetSystem<ExpUpgradeSystem>();
        if (system != null && system.items.Count>0)
        {
            if (isIncrement) system.items[(int)abilityEnum].Weight += weight;
            else system.items[(int)abilityEnum].Weight = weight;
#if  UNITY_EDITOR
            Debug.Log("能力:"+abilityEnum+"已改变出现权重:"+weight);
#endif
            Global.DDALog.Value += $"\n{Mathf.Floor(Global.CurrentTime.Value)} {(isIncrement?"Increase":"set")} Ability{abilityEnum} weight:{weight}";
            return true;
        }
        return false;
    }

    public Type InputType()
    {
        return typeof(int);
    }

    public void ReceiveObject(object obj)
    {
        this.abilityEnum = (AbilityEnum)obj;
    }
}

public class DDASetNextDropItemType : DDAAction
{
    [ShowInInspector, LabelText("能力")] public DropItemEnum dropItemEnum;
    public override bool Invoke()
    {
        base.Invoke();
        #if UNITY_EDITOR
        Debug.Log("下次掉落强制掉落："+dropItemEnum);
        #endif
        Global.DDALog.Value += $"\n{Mathf.Floor(Global.CurrentTime.Value)} Next DropItem is:{dropItemEnum}";
        var ins = DropItemManager.Instance;
        if (ins != null) 
        {
            ins.SetNextDropItem((int)dropItemEnum);
        }
        return true;
    }
}

public class DDASetEnemyWeight : DDAAction
{
    public override string Description => "key为敌人id，value为敌人权重";

    [ShowInInspector, LabelText("敌人列表")]
    public List<(int,int)> enemyWeights = new List<(int, int)>();

    [ShowInInspector, LabelText("是否将为未设置的置位0")]
    public bool isReset = false;

    [ShowInInspector, LabelText("是否为增量")] 
    public bool isIncrement = false;


    public override bool Invoke()
    {
        base.Invoke();
        if (EnemyGenerator.Instance)
        {
            if (isReset)
            {
                foreach (var value in EnemyGenerator.Instance.generateInfoList)
                {
                    value.generateWeight = 0;
                }
            }

            foreach (var value in enemyWeights)
            {
                var (id, weight) = value;
                EnemyGenerator.Instance.SetGenerateWeight(id,weight,isIncrement);
                Global.DDALog.Value += $"\n{Mathf.Floor(Global.CurrentTime.Value)} Set GenerateEnemy Weight:{id}:{weight}";
            }

        }
        return true;
    }
}

public class DDAGenerateEnemy : DDAAction
{
    [ShowInInspector, LabelText("敌人ID")] public int enemyID;
    [ShowInInspector, LabelText("数量")] public int num;
    public override bool Invoke()
    {
        base.Invoke();
        if (EnemyGenerator.Instance)
        {
#if UNITY_EDITOR
            Debug.Log("召唤敌人："+enemyID+":"+num);
#endif
            Global.DDALog.Value += $"\n{Mathf.Floor(Global.CurrentTime.Value)} GenerateEnemy:{enemyID}:{num}";
            for (int i = 0; i < num; i++)
            {
                
                EnemyGenerator.Instance.GenerateEnemy(enemyID);
            }
        }
        return true;
    }
}
